﻿using System.Data;
using System.Web.UI;
//using EasyLib.DB.AbstractLayer;
//using NopSolutions.NopCommerce.Common.Utils;
using System.Text.RegularExpressions;
using System;
using System.Web;
using System.Globalization;
using System.Collections;
using System.Web.Caching;
using Tour.DAL;
using Tour.BLL;
using System.Collections.Generic;

namespace VST.Controls
{
    public class BaseLCGUserControl : UserControl
    {
        static string keyResource = "Resource";
        public static BaseLCGUserControl CreateInstant()
        {
            return new BaseLCGUserControl();
        }
        public string GetLocaleResourceString(string resourceName)
        {
            if (HttpContext.Current.Cache[keyResource] == null)
            {
                CacheLocaleResourceString();
            }
            Hashtable ht = (Hashtable)HttpContext.Current.Cache[keyResource];
            string key = WorkingLanguage + "_" + resourceName;
            if (ht[key] != null)
                return ht[key].ToString();
            return "";
        }
        void CacheLocaleResourceString()
        {
            Hashtable ht = GetAllLocaleResourceString();
            Cache.Insert(keyResource, ht, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero, CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));
        }
        private void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason)
        {
            if (HttpContext.Current.Cache[keyResource] == null)
            {
                CacheLocaleResourceString();
            }
        }
        Hashtable GetAllLocaleResourceString()
        {
            Hashtable resource = new Hashtable();
            List<LocaleStringResourceEntity> ls = LocaleStringResourceManager.CreateInstant().SelectAll();
            for (int i = 0; i < ls.Count; i++)
            {
                string key = ls[i].LanguageID + "_" + ls[i].ResourceName;
                string value = ls[i].ResourceValue;
                resource.Add(key, value);
            }
            return resource;
        }

        //public static string GetLocaleResourceChannel(string channelTextID)
        //{
        //    //DBAbstract db = new DBAbstract();
        //    //db.ParseFromWebConfig("");
        //    //DataSet a1 = db.Query("[sys_GetResourceChannel] @sChannelTextID='" + channelTextID + "',@LanguageID=" + WorkingLanguage);

        //    //if (a1 != null && a1.Tables[0].Rows.Count > 0)
        //    //{
        //    //    return a1.Tables[0].Rows[0]["Name"].ToString();
        //    //}
        //    return "";
        //}

        //public string GetMessageString()
        //{
        //    string msgCode = Request.QueryString["MsgCode"];
        //    if (string.IsNullOrEmpty(msgCode))
        //    {
        //        if (Session["MsgCode"] == null)
        //            return "";
        //        else
        //        {
        //            msgCode = Session["MsgCode"].ToString();
        //            if (string.IsNullOrEmpty(msgCode))
        //                return "";
        //        }
        //    }

        //    DBAbstract db = new DBAbstract();
        //    db.ParseFromWebConfig("");
        //    DataSet a1 = db.Query("[sys_GetMessageString] @MessageCode='" + msgCode + "',@iLanguageID=" + WorkingLanguage);
        //    if (a1 != null && a1.Tables[0].Rows.Count > 0)
        //    {
        //        DataRow row = a1.Tables[0].Rows[0];
        //        string value = row["Value"].ToString();
        //        string cssClass = row["CssClass"].ToString();

        //        Session["MsgCode"] = null;
        //        return string.Format("<p class=\"MessageString {1}\">{0}</p>", Server.HtmlEncode(value), cssClass);
        //    }
        //    return "";

        //}

        public static int WorkingLanguage
        {
            get
            {
                if (CommonHelper.GetCookieInt("VST.Language") > 0)
                    return CommonHelper.GetCookieInt("VST.Language");
                else
                    return 1;
            }
        }

        public static string GetLimitHead(string head, int length)
        {
            string result = head;
            if (result.Length > length)
            {
                result = result.Substring(0, length);
                result = result.Substring(0, result.LastIndexOf(" "));
                result = result + " ...";
            }

            return result;
        }
        public string getCreatedDate(string pubTime)
        {
            string strDate = "";
            DateTime artDate = DateTime.Parse(pubTime);
            if (WorkingLanguage == 1)
                strDate = String.Format("{0} {1}", artDate.ToString("dd/MM/yyyy"), artDate.ToString("hh:mm"));
            else
                strDate = String.Format("{0} {1}", artDate.ToString("MM/dd/yyyy"), artDate.ToString("hh:mm"));
            return strDate;
        }
        public string getDetailNews(string ChannelTextID, string NewsTextID)
        {
            if (ChannelTextID != "" && NewsTextID != "")
                return "/" + ChannelTextID + "/" + NewsTextID + ".htm";
            return "#";
        }
        //public string getDetailVSTNews(string ChannelID, int ArticleID, string Title)
        //{
        //    FriendlyURL strURL = new FriendlyURL();

        //    if (ChannelID != "" && ArticleID > 0)
        //        return "/" + GetLocaleResourceString("User.ResearchCenter") + "/" + GetLocaleResourceString("User.ResearchCenter.UserControl.ThiTruongNiemYet")
        //                + "/" + ChannelID + "/" + ArticleID + "-" + strURL.FormatTitle(Title)+".htm";
        //    return "#";
        //}
        public string getDocURL(string NewsTextID)
        {
            if (NewsTextID != "")
                return "javascript:downloadFile('" + NewsTextID + "');";
            return "#"; //new CultureInfo().NumberFormat
        }
        public string getURLChannel(string ChannelTextID)
        {
            //DBAbstract db = new DBAbstract();
            //db.ParseFromWebConfig("");
            //DataSet ch = db.Query("[art_SelectTblChannelByTextId] @TextId='" + ChannelTextID + "'");

            //if (ch != null && ch.Tables[0].Rows.Count > 0)
            //{
            //    return "<a href='" + ch.Tables[0].Rows[0]["Url"].ToString() + "'>" + ch.Tables[0].Rows[0]["Name"].ToString() + "</a>";
            //}
            return "";
        }
        public string removeTitleHead(string content)
        {
            string txtContent;
            txtContent = content.Replace("ImageView.aspx", "/ImageView.aspx");
            txtContent = Regex.Replace(txtContent, "(<p class=pTitle|<P class=pTitle)[^>]*>(.*?)(</P>|</p>)", "", RegexOptions.Singleline);
            txtContent = Regex.Replace(txtContent, "(<p class=pHead|<P class=pHead)[^>]*>(.*?)(</P>|</p>)", "", RegexOptions.Singleline);
            return txtContent;
        }
        //protected string FormatChannel(object channel)
        //{
        //    //string[] arr = channel.Split('>');
        //    string s = Regex.Replace(channel.ToString(), ">", "<span style='font-weight:normal !important'> &gt; </span>");
        //    return s;
        //}

        public NumberFormatInfo GetNumberFormat()
        {
            if (WorkingLanguage == 1)
                return CultureInfo.GetCultureInfo("vi-VN").NumberFormat;
            return CultureInfo.GetCultureInfo("en-US").NumberFormat;
        }
    }
}